package com.example.fashionshoes.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fashionshoes.mapper.CategoryMapper;
import com.example.fashionshoes.pojo.po.Category;
import com.example.fashionshoes.pojo.po.CategoryGoods;
import com.example.fashionshoes.pojo.po.Goods;
import com.example.fashionshoes.pojo.vo.CategoryGoodsVO;
import com.example.fashionshoes.pojo.vo.GoodsDetailVO;
import com.example.fashionshoes.service.CategoryService;
import com.example.fashionshoes.service.GoodsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private GoodsService goodsService;

    /**
     * 获取所以分类
     * @return  分类数据
     */
    @Override
    public List<Category> getCategory() {
        //TODO 使用redis优化性能
        //父分类
        List<Category> categoryList = lambdaQuery().eq(Category::getIsParent, 0).list();
        //子分类
        categoryList.forEach(category -> {
            List<Category> subCategory = lambdaQuery().eq(Category::getParentId, category.getId()).list();
            //子子分类
            subCategory.forEach(sb-> sb.setSubCategory(lambdaQuery().eq(Category::getIsParent, 1).eq(Category::getParentId, sb.getId()).list()));
            category.setSubCategory(subCategory);
        });
        return categoryList;
    }
    /**
     * 获取二级分类列表和商品
     * @param id 分类id
     * @return 分类列表和商品
     */
    @Override
    public CategoryGoodsVO getCategoryAndGoods(Integer id) {
        //获取一级分类
        Category category = getById(id);
        //获取一级分类对应的二级分类
        List<Category> subCategoryList = lambdaQuery().eq(Category::getParentId, id).list();
        List<CategoryGoods> categoryGoodsList = toIterator(subCategoryList,Goods::getCategoryId);
        CategoryGoodsVO categoryGoodsVo = BeanUtil.copyProperties(category, CategoryGoodsVO.class);
        categoryGoodsVo.setCategoryGoodsList(categoryGoodsList);
        return categoryGoodsVo;
    }

    /**
     * 获取二级分类
     * @param id 一级分类id
     * @return 二级分类
     */
    @Override
    public CategoryGoodsVO getCategoryAndSubCategory(Integer id) {
        Category category = getById(id);
        CategoryGoodsVO categoryGoodsVo = BeanUtil.copyProperties(category, CategoryGoodsVO.class);
        Category parentCategory = lambdaQuery().eq(Category::getId, category.getParentId()).one();
        categoryGoodsVo.setParentName(parentCategory.getName());
        return categoryGoodsVo;
    }
    /**
     * 获取所有商品
     * @return 所有商品包含分类id
     */
    @Override
    public List<CategoryGoods> getList() {
        List<Category> categoryList = lambdaQuery().eq(Category::getIsParent, 0).list();
        return toIterator(categoryList,Goods::getType);
    }
    /**
     * 获取指定商品包含分类
     * @param id 商品id
     * @return 指定商品
     */
    @Override
    public GoodsDetailVO getGoodsDetail(Integer id) {
        GoodsDetailVO goodsDetail = categoryMapper.getGoodsDetail(id);
        goodsDetail.setSkusId(id);
        goodsDetail.setSpecsId(id);
        Category parentCategory = lambdaQuery().eq(Category::getId, goodsDetail.getParentId()).one();
        goodsDetail.setCategoryParentId(parentCategory.getId());
        goodsDetail.setCategoryParentName(parentCategory.getName());
        return goodsDetail;
    }

    /**
     * 根据传入的类型，动态查询数据库的商品
     * @param categoryList 二级分类
     * @param function 传入的字段
     * @return 商品及其分类
     */
    private List<CategoryGoods> toIterator(List<Category> categoryList, SFunction<Goods,?> function){
        List<CategoryGoods> categoryGoodsList = new ArrayList<>();
        categoryList.forEach(category -> {

            List<Goods> goodsList = goodsService.lambdaQuery().eq(function, category.getId()).list();
            CategoryGoods categoryGoods = BeanUtil.copyProperties(category, CategoryGoods.class);
            categoryGoods.setGoodsList(goodsList);
            categoryGoodsList.add(categoryGoods);
        });
        return categoryGoodsList;
    }
}
